package com.share.domain.strategy.service.rule.chain.factory;

import com.share.domain.strategy.model.entity.StrategyEntity;
import com.share.domain.strategy.repository.IStrategyRepository;
import com.share.domain.strategy.service.rule.chain.ILogicChain;
import lombok.*;
import org.springframework.stereotype.Service;

import java.util.Map;
@Service
public class DefaultChainFactory {
    private final Map<String, ILogicChain> logicChainGroup;
    private final IStrategyRepository repository;

    public DefaultChainFactory(Map<String, ILogicChain> logicChainGroup,IStrategyRepository repository){
        this.logicChainGroup=logicChainGroup;
        this.repository=repository;
    }

    public ILogicChain openLogicChain(String userId, Long strategyId){
        StrategyEntity strategy = repository.queryStrategyEntitiesByStrategyId(strategyId);
        String[] ruleModels = strategy.ruleModels();

        //如果未配置规则 装配默认责任链
        if(null==ruleModels|| ruleModels.length==0)return logicChainGroup.get("default");

        //按照配置顺序装填用户配置的责任链
        ILogicChain logicChain = logicChainGroup.get(ruleModels[0]);
        ILogicChain current = logicChain;
        for (int i = 1; i < ruleModels.length; i++) {
            ILogicChain nextChain = logicChainGroup.get(ruleModels[i]);
            current = current.appendNext(nextChain);
        }

        //责任链最后装填默认责任链
        current.appendNext(logicChainGroup.get("default"));
        return logicChain;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class StrategyAwardVo{
        private Integer awardId;
        private String logicModel;
        private String awardRuleValue;

    }

    @Getter
    @AllArgsConstructor
    public enum LogicModel{
        RULE_DEFAULT("rule_default","默认抽奖"),
        RULE_BLACKLIST("rule_blacklist","黑名单抽奖"),
        RULE_WEIGHT("rule_weight","权重规则"),
        ;

        private final String code;
        private final String info;
    }
}
